<template>
  <div class="overview">
    <div v-if="false">{{testData}}</div>
    <div class="info">
      <img id="userHead" :src="overview.userHead" @click.stop.prevent="setUserHeader">
      <div class="sub">
        <img src="~assets/images/lianjie@3x.png">
        <a href="javascript:;" @click="toFans">
          <div class="one">订阅了我</div>
          <div class="two">
            <img src="~assets/images/dingyue@3x.png">
          </div>
          <div class="three">
            <span id="subscribeUserCount">{{overview.subscribeUserCount}}</span>
            <span class="unit">人</span>
          </div>
        </a>
      </div>

      <div class="praise clearfix">
        <img src="~assets/images/lianjie@3x.png">
        <a href="javascript:;" @click.stop.prevent="toGoodPoint">
          <div class="one">称赞了我</div>
          <div class="two">
            <i class="iconfont icon-dianzan"></i>
          </div>
          <div class="three">
            <span id="goodpointCount">{{overview.goodpointCount}}</span>
            <span class="unit">人</span>
          </div>
        </a>
      </div>

      <div class="message">
        <img src="~assets/images/lianjie@3x.png">
        <a href="gtjanrng://jumpToMessagecenter">
          <div class="one">消息</div>
          <div class="two">
            <i class="iconfont icon-xiaoxi"></i>
          </div>
          <div class="three" id="messageNum">{{overview.messageNum}}</div>
        </a>
      </div>
    </div>

    <div class="one">
      <span class="nickname" id="nickname">{{overview.nickname}}</span>
      <div class="gross-rate" :class="overview.totalRate<0?'negative':'positive'">
        <div>总盈利率</div>
        <div id="totalRate">{{overview.totalRate}}%</div>
      </div>
    </div>

    <table class="two table">
      <tr class="">
        <td>
          <div class="row">
            <a class="col-xs-3 no-padding mt-7" href="javascript:;" @click.stop.prevent="toTask">
              <img src="~assets/images/junyuanbao@3x.png" style="width: 40px">
            </a>
            <a class="col-xs-5 no-padding mt-7 text-left" href="javascript:;" @click.stop.prevent="toTask">
              <div class="points">
                <div class="value" id="walletTotal">{{overview.walletTotal}}</div>
                <div class="key">君元宝</div>
              </div>
              <i class="iconfont icon-xiangyou"></i>
            </a>
            <a class="col-xs-4 no-padding mall relative mt-7" href="gtjayyz://jumpfunc?funid=11002">
              <img src="~assets/images/duihuan@2x.png" class="img-responsive">
              <div class="tips">去商城兑换</div>
            </a>
          </div>
        </td>

        <td @click.stop.prevent="toMedalsWall">
          <medals-owned :medalsProps="medalsOwned" :userType="'self'"></medals-owned>
        </td>
      </tr>
    </table>

    <div class="row three">
      <div class="col-xs-4 no-padding mt-7">
        <div class="value" id="fundBalance">{{overview.fundBalance}}</div>
        <div class="key">模拟总资产</div>
      </div>

      <div class="col-xs-4 no-padding mt-7">
        <div class="value" id="enableBalance">{{overview.enableBalance}}</div>
        <div class="key">模拟可用资产</div>
      </div>

      <div class="col-xs-4 no-padding three-right">
        <div class="value" id="dayRate" :class="overview.dayRate<0?'negative':'positive'">{{overview.dayRate}}%</div>
        <div class="key">日盈亏率</div>
        <div class="time">更新时间 {{overview.updateTime}}</div>
      </div>
    </div>

    <img src="~assets/images/junyuanbao.png" id="signInSuccess">

    <!--yanqiong delete -->
    <!--<div class="row four">-->
    <!--<div class="col-xs-offset-3 col-xs-6">-->
    <!--<button type="button" class="btn btn-sign-in btn-block" id="signIn" :data-done="hasInitSignIn?'1':'0'">签 到</button>-->
    <!--</div>-->
    <!--</div>-->
  </div>
</template>

<script>
  import RemoteDAO from 'common/remoteDAO'
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'
  import MedalsOwned from 'components/dynamic/MedalsOwned'
  import Constant from 'common/constant'
  import _ from 'underscore'

  export default {
    data () {
      return {
        overview: {
          nickname: LocalDAO.getNickname(),
          userHead: LocalDAO.getUserHead(),
          subscribeUserCount: '--', //订阅数
          goodpointCount: '--', // 被点赞数
          messageNum: '--', // 信息数
          totalRate: '--', // 总盈亏率
          fundBalance: '--', // 模拟资产
          enableBalance: '--', // 可用资产
          dayRate: '--', // 日盈亏率
          walletTotal: '--', // 君元宝数
          updateTime: ''
        },
        medalsOwned: LocalDAO.getMedalsOwned() || {list: []}, // 勋章
        subscribePrice: '--', // 被订阅价格
        hasInitSignIn: false, //是否签到
        testData: 'testData'
      }
    },
    props: {
      userCode: {
        type: String
      }
    },
    components: {
      'medals-owned': MedalsOwned,
    },
    created: function () {
      this.fetchData();
//      this.initSignIn();
    },
    methods: {
      fetchData(){
        var self = this;
        //查询用户资产
        RemoteDAO.callJCL11103(self.userCode, function (response) {
          if (Number(response.error) === 0) {
            var data = response.data.body.data[0];
            self.overview.updateTime = CommonUtil.formatNewHMTime();
            self.overview.totalRate = CommonUtil.toFixed(data.total_rate, 2);// 总盈亏率
            self.overview.fundBalance = CommonUtil.toFixed(data.fund_balance, 2); // 模拟资产
            self.overview.enableBalance = CommonUtil.toFixed(data.enable_balance, 2); // 可用资产
            self.overview.dayRate = CommonUtil.toFixed(data.day_rate, 2); // 日盈亏率
            self.overview.totalFloatProfit = CommonUtil.toFixed(data.total_floatprofit, 2); // 累计盈亏
            if(self.overview.dayRate>=0){
              self.overview.dayRate = "+" + self.overview.dayRate;
            }
            if(self.overview.totalFloatProfit>=0){
              self.overview.totalFloatProfit = "+" + self.overview.totalFloatProfit;
            }

            LocalDAO.setTotalRate(self.overview.totalRate);
            LocalDAO.setDayRate(self.overview.dayRate);
            LocalDAO.setTotalFloatProfit(self.overview.totalFloatProfit);
            LocalDAO.setFundBalance(self.overview.fundBalance);
            LocalDAO.setEnableBalance(self.overview.enableBalance);

            // 更新分享页数据
            var shareSelfDetails = LocalDAO.getShareSelfDetails();
            shareSelfDetails.overview.totalRate = self.overview.totalRate + "%";
            shareSelfDetails.overview.dayRate = self.overview.dayRate + "%";
            shareSelfDetails.overview.totalFloatProfit = self.overview.totalFloatProfit;
            shareSelfDetails.overview.fundBalance = self.overview.fundBalance;
            shareSelfDetails.overview.enableBalance = self.overview.enableBalance;
            LocalDAO.setShareSelfDetails(shareSelfDetails);
          } else {
            self.$store.dispatch('setTip', {text: response.data});
          }
        });

        //查询用户资料
        RemoteDAO.callUC12000104(self.userCode, function (response) {
          //self.testData = JSON.stringify(response);
          if (Number(response.error) === 0) {
            self.overview.userHead = response.data.body[0].USER_HEAD || CommonUtil.getDefaultUserHead(self.userCode);
            self.overview.nickname = response.data.body[0].NICKNAME;
            self.overview.goodpointCount = response.data.body[0].GOODPOINT_COUNT;
            self.overview.subscribeUserCount = response.data.body[0].SUBSCRIBE_USER_COUNT;
            self.overview.walletTotal = CommonUtil.toFixed(response.data.body[0].WALLET_TOTAL, 0);
            self.subscribePrice = CommonUtil.toFixed(response.data.body[0].SUBSCRIBE_PRICE, 0) || 0;

            // 跟新本地缓存
            LocalDAO.setUserHead(self.overview.userHead);
            LocalDAO.setNickname(self.overview.nickname);
            LocalDAO.setWalletTotal(self.overview.walletTotal);
            LocalDAO.setGoodpointCount(self.overview.goodpointCount);
            LocalDAO.setSubscribeUserCount(self.overview.subscribeUserCount);
            LocalDAO.setSubscribePrice(self.subscribePrice);

            var list = [];//获取已得勋章
            try {
              var honorList = JSON.parse(response.data.body[0].HONOR_LIST);
              _.each(honorList, function (item, index) {
                var object = {};
                object.medalCode = item.HONOR_CODE;
                object.medalName = item.HONOR_NAME;
                object.iconURL = ""; //
                object.walletNum = ""; // 获得勋章奖励金额
                list.push(object);
              });
            } catch (err) {
              self.$store.dispatch('setTip', {text: "解析已获取勋章列表返回值失败"});
            }
            //查询所有勋章,获取各勋章详细资料
            RemoteDAO.callUC12000042(self.userCode, "", function (resp) {
              if (Number(resp.error) === 0) {
                _.each(resp.data.body, function (item, index) {
                  var object = {};
                  object.iconURL = item.ICON_URL;  //勋章图地址
                  object.honorName = item.HONOR_NAME; //勋章名称
                  object.honorCode = item.HONOR_CODE; //勋章代码
                  object.configType = item.CONFIG_TYPE; //勋章类型
                  object.walletNum = item.CONFIG_VALUE.WALLET_NUM; //奖励君元宝数
                  object.taskDesc = item.CONFIG_VALUE.TASK_DESC; //任务说明
                  // 为 medalsOwned 赋值勋章图片路径和获得勋章奖励君元宝数
                  _.each(list, function (item, index) {
                    if (item.medalCode == object.honorCode) {
                      item.iconURL = object.iconURL;
                      item.walletNum = object.walletNum;
                      return false
                    }
                  });
                });

                //最后得到字段完全的已获勋章列表
                self.medalsOwned.list = list;
                LocalDAO.setMedalsOwned(self.medalsOwned);

                // 更新分享页数据
                var shareSelfDetails = LocalDAO.getShareSelfDetails();
                shareSelfDetails.overview.nickname = self.overview.nickname;
                shareSelfDetails.overview.userHead = self.overview.userHead == '' ? CommonUtil.getDefaultUserHead(self.userCode) : self.overview.userHead;
                shareSelfDetails.overview.walletTotal = self.overview.walletTotal;
                shareSelfDetails.overview.goodpointCount = self.overview.goodpointCount;
                shareSelfDetails.overview.subscribeUserCount = self.overview.subscribeUserCount;
                shareSelfDetails.overview.subscribePrice = self.subscribePrice;
                shareSelfDetails.medalsOwned = self.medalsOwned;
                LocalDAO.setShareSelfDetails(shareSelfDetails);
              } else {
                self.$store.dispatch('setTip', {text: resp.data});
              }
            });
          } else {
            self.$store.dispatch('setTip', {text: response.data});
          }
        });

        // 查询信息数
        RemoteDAO.callNativeMessageNum(function (response) {
          if (Number(response.error) === 0) {
            self.overview.messageNum = response.data.message_no;
            // 更新分享页数据
            var shareSelfDetails = LocalDAO.getShareSelfDetails();
            shareSelfDetails.overview.messageNum = self.overview.messageNum;
            LocalDAO.setShareSelfDetails(shareSelfDetails);
          }
        });

      },
      /**
       * 跳转勋章墙
       * @param e
       */
      toMedalsWall (e) {
        var self = this
        self.$router.push({path: '/medalswall', query: {userType: Constant.user.SELF}});
      },
      /**
       * 跳转称赞了我的用户页面
       */
      toGoodPoint(){
        var self = this;
        //self.$store.dispatch('setTip', {text: '画面待写'});
        self.$router.push({path: '/admirers'})
      },
      /**
       * 跳转粉丝页
       */
      toFans(){
        var self = this;
        //self.$store.dispatch('setTip', {text: '画面待写'});
        self.$router.push({path: '/fans'})
      },
      /**
       * 跳转任务页
       */
      toTask(){
        var self = this;
        //self.$store.dispatch('setTip', {text: '画面待写'});
        self.$router.push({path: '/task'})
      },
      /**
       * 查询是否签到
       */
      initSignIn (){
        var self = this;
        var args = {};
        args.USER_CODE = self.userCode;
        args.BEHAVIOR_FLAG = "NGQD";
        args.SYS_SOURCE = "02";
        args.BEHAVIOR_DATE = CommonUtil.getTodayFormatDate();

        //查询是否签到
        RemoteDAO.callUC12000023(args, function (response) {
          if (response.error == "0") {
            if (response.data.body.length > 0) {
              self.hasInitSignIn = true;
            } else {
              self.hasInitSignIn = false;
            }
          } else {
            self.$store.dispatch('setTip', {text: response.data});
          }
        });
      },
      /**
       * 签到操作
       */
      signIn (e){
        var self = this;
        var target = e.currentTarget;
        var done = target.getAttribute('data-done');
        if (done == "") { // 接口尚未返回签到状况
          self.$store.dispatch('setTip', {text: "网速较慢，请稍后！"});
          if (!self.hasInitSignIn) { //初始化签到状态失败,再次获取
            self.initSignIn();
          }
        } else if (done == "1") {
          self.$store.dispatch('setTip', {text: "大王你今日已签到过了~"});
        } else {
          RemoteDAO.callUC12000022(self.userCode, "A", "6", "", "NGQD", "02", "", function (response) {
            if (response.error == "0") {
              var walletTotalOld = LocalDAO.getWalletTotal();
              var walletTotalNew = Number(walletTotalOld) + Number(2); // 固定增加积分
              self.walletTotal = walletTotalNew; // 更新页面上的总君元宝数
              self.hasInitSignIn = false;
              LocalDAO.setWalletTotal(walletTotalNew); // 更新 localStorage 中的总君元宝数
              $("#signInSuccess").show().animate({top: "-100px", left: "-70px"}, 1000, function () {
                $(this).css({top: "0", left: "0"}).hide();
              });
            } else {
              self.$store.dispatch('setTip', {text: response.data});
            }
          });
        }
      },
      /**
       * 跳转设置头像
       */
      setUserHeader(){
        var self = this
        RemoteDAO.callNativeSetHeader(function (response) {
          if (Number(response.error) === 0) {
            self.userHead = response.data.userhead;
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>
